<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page isELIgnored="false" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>无标题文档</title>
    <link href="css/common.css" rel="stylesheet" type="text/css"/>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="wrap">

    <!-- 在合适位置添加错误提示显示区域 -->
    <c:if test="${not empty errorMsg}">
        <div class="error-message" style="color: red; margin: 10px 0; padding: 10px; border: 1px solid red;">
                ${errorMsg}
        </div>
    </c:if>
    <!-- main begin-->

    <div class="sale">
        <h1 class="lf">在线拍卖系统</h1>
        <div class="logout right"><p>${auctionuser.userName}</p></div>
        <div class="logout right"><a href="login.jsp" title="注销">注销</a></div>
    </div>
    <div class="items sg-font lf">
        <ul class="rows">
            <li>名称：</li>
            <li class="borderno">${auction.auctionName}</li>
        </ul>
        <ul class="rows">
            <li>描述：</li>
            <li class="borderno">${auction.auctionDesc}</li>
        </ul>
        <ul class="rows">
            <li>开始时间：</li>
            <li class="borderno">${auction.auctionStartTime}</li>
        </ul>
        <ul class="rows">
            <li>结束时间：</li>
            <li class="borderno">${auction.auctionEndTime}</li>
        </ul>
        <ul class="rows border-no">
            <li>起拍价：${auction.auctionStartPrice}</li>
            <li class="borderno">2.000</li>
        </ul>
    </div>
    <div class="rg borders"><img src="${auction.auctionPic}" width="270" height="185" alt=""/></div>
    <div class="cl"></div>
    <div class="top10 salebd">
        <p>
            <label for="sale">出价：</label>
            <input type="text" class="inputwd" id="sale" value="0.0"/>
            <input name="" type="submit" value="竞 拍" class="spbg buttombg f14  sale-buttom" onclick="bidDingPage()"/>
        </p>
        <span class="f14 red" id="auctionError" style="display:none">不能低于最高竞拍价</span>
    </div>
    <div class="top10">
        <input name="" type="submit" value="刷 新" class="spbg buttombg f14"
               onclick="window.location = window.location.href"/>
        <input name="" type="submit" value="返回列表" class="spbg buttombg f14"
               onclick="window.location.href=`/auction/do/page?userIsAdmin=${userIsAdmin}`"/>

        <input name="" type="submit" value="查询竞拍结果" class="spbg buttombg f14"
               onclick="window.location.href = '/auction/do/findAuctionrecordAll'"/>
    </div>
    <div class="offer">
        <h3>出价记录</h3>
        <div class="items sg-font">
            <div class="rows even strong">
                <li>竞拍人</li>
                <li>竞拍时间</li>
                <li class="borderno">竞拍价格</li>
            </div>
            <c:forEach items="${auctionrecords}" var="record">
                <ul class="rows even strong">
                    <li>${record.user.userName}</li>
                    <li>${record.auctionTime}</li>
                    <li class="borderno">${record.auctionPrice}</li>
                </ul>
            </c:forEach>
        </div>
    </div>
    <!-- main end-->
</div>
<script>
    // 获取结束日期
    const endDateStr = '${auction.auctionEndTime}'; // JSP EL表达式获取结束时间
    console.log(endDateStr)
    // 在页面加载时将最高价赋给JS变量  不是最高竞价 就是起拍价
    var highestPrice = <c:choose>
        <c:when test="${not empty auctionrecords}">
        ${auctionrecords[0].auctionPrice}
        </c:when>
        <c:otherwise>
        ${auction.auctionStartPrice}
        </c:otherwise>
        </c:choose>

        function bidDingPage() {

            // 获取用户输入的竞拍价格
            const bidPriceInput = document.getElementById('sale');
            const bidPriceStr = bidPriceInput.value.trim(); // 去除前后空格

            if (!bidPriceStr) {
                alert("请输入竞拍价格");
                bidPriceInput.focus();
                return false;
            }

            const bidPrice = parseFloat(bidPriceStr);
            if (isNaN(bidPrice)) {
                alert("请输入有效的数字价格");
                bidPriceInput.focus();
                return false;
            }

            // 验证竞拍价格是否合法
            const auctionError = document.getElementById('auctionError');
            //不能低于竞拍用户最高竞拍价或者是起拍价
            if (bidPrice < highestPrice) {
                auctionError.style.display = 'inline';
                return false;
            }
            //现在的竞拍时间
            // 使用ISO格式日期
            const currentTime = new Date().toISOString();
            // 跳转到编辑页面
            window.location.href = "/auction/do/insertAuctionrecord?auctionTime=" + currentTime + "&auctionPrice=" + bidPrice;
        }
</script>
</body>
</html>
